﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using JDML.TestPoco;
using JDML.StaticExtensions;
using System.Data.SqlClient;
using System.Linq;
using System.Collections.ObjectModel;
using JDML.BindingList;

namespace JDML
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            UserRole role = new UserRole();
            //role.ID = 2;
            role.Name = "Manage";
            //role.ID = 1;
            if (role.Find())
            {
                MessageBox.Show(role.UpdateTime.GetValueOrDefault().ToString());
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            UserRole role = new UserRole();
            //role.ID = 1;
            role.Name = "Sales";
            IList<UserRole> list = role.FindList<UserRole>();
            foreach (Object one in list)
            {
                UserRole onerole = one as UserRole;
                Console.WriteLine("Role_ID:" + onerole.ID + ", Role_Name:" + onerole.Name + ", Role_Permission:" + onerole.Permission + ", UpdateTime:" + onerole.UpdateTime.GetValueOrDefault().ToString());
            }
            dataGridView1.DataSource = list;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            UserRole role = new UserRole();
            //role.ID = 1;
            role.Name = "Sales";
            IList<UserRole> list = role.FindList<UserRole>("byName2");
            foreach (Object one in list)
            {
                UserRole onerole = one as UserRole;
                Console.WriteLine("Role_ID:" + onerole.ID + ", Role_Name:" + onerole.Name + ", Role_Permission:" + onerole.Permission + ", UpdateTime:" + onerole.UpdateTime.GetValueOrDefault().ToString());
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            UserRole newrole = new UserRole();
            newrole.Name = "bbc";
            newrole.Permission = "32423423";
            newrole.UpdateTime = DateTime.Now;
            newrole.Insert();
            Console.WriteLine(newrole.ID);
            MessageBox.Show(newrole.ID.GetValueOrDefault().ToString());
        }

        private void button5_Click(object sender, EventArgs e)
        {
            UserRole role = new UserRole() { ID = 18 };
            role.Permission = "ttt";
            role.Update();
            Console.WriteLine(role.Name);
        }

        private void button6_Click(object sender, EventArgs e)
        {
            UserRole role = new UserRole() { ID = 17 };
            role.Delete();
        }

        private void button7_Click(object sender, EventArgs e)
        {
            User_Role user_role = new User_Role() { User_Role_Name = "Manage" };
            if(user_role.Find())
            {
                Console.WriteLine(user_role.User_Role_Permission);
            }
        }

        private void button8_Click(object sender, EventArgs e)
        {
            UserRole role = new UserRole();
            //role.ID = 2;
            //role.Name = "Manage";
            role.ID = 1;
            if (role.Load())
            {
                MessageBox.Show(role.UpdateTime.GetValueOrDefault().ToString());
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            DAOExtension.Connection = EstablishConnection.getSQLConn();
        }

        private void button9_Click(object sender, EventArgs e)
        {
            string sqlstr = "select * from User_Role where User_Role_ID = -1 ";
            SqlDataAdapter da = new SqlDataAdapter(sqlstr, EstablishConnection.getSQLConn());
            DataSet ds = new DataSet();
            da.Fill(ds);
            Console.WriteLine(ds.Tables[0].Columns.Count);
            //SqlCommand
        }

        private void btn_CreateTable_Click(object sender, EventArgs e)
        {
            (new CustomerSource()).CreateTable();
            
        }

        private void button10_Click(object sender, EventArgs e)
        {
            IList<User_Role> roles = new SortedBindingList<User_Role>();
            roles.Add(new User_Role() { User_Role_Name = "1", User_Role_ID = 1 });
            roles.Add(new User_Role() { User_Role_Name = "2", User_Role_ID = 2 });
            roles.Add(new User_Role() { User_Role_Name = "3", User_Role_ID = 3 });
            dataGridView1.DataSource = roles;
        }

        private void button11_Click(object sender, EventArgs e)
        {
            (dataGridView1.DataSource as SortedBindingList<User_Role>).Add(new User_Role() { User_Role_ID = 4 });
        }
    }
}
